﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.UIElement.Table.Structure.Element;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace BoolanElement.UI.Table
{
    public delegate void StyleUnitChangedHandler(BooTableStyleUnit styleUnit, int row,int column,int changedType);
    public delegate void StyleUnitsChangedHandler(BooTableStyleUnit styleUnit, List<Point> units, int changedType);
    public class BooTableStyle
    {
        public List<List<BooTableElementUnit>> Style;
        public BooBorderStyle borderStyle;
        public BooTableStyle(BooTableElement element)
        {
            Style = element.Units;
            borderStyle = new BooBorderStyle();
        }

        public void ResizeColumnWidth(int column,double width)
        {
            foreach (List<BooTableElementUnit> row in Style)
            {
                row.ElementAt(column).Style.Width = width;
            }
        }

        public void ResizeRowHeight(int row, double height)
        {
            foreach (BooTableElementUnit unit in Style.ElementAt(row))
            {
                unit.Style.Height = height;
            }
        }

        public void SetStyle(BooTableStyleUnit data, int row, int column)
        {
            int rowCount = Style.Count;
            int columnCount = Style.First().Count;
            if (row < 0 || row > rowCount - 1)
                return;
            if (column < 0 || column > columnCount - 1)
                return;
            List<BooTableElementUnit> rowData = Style.ElementAt(row);
            rowData[column].Style = data;
 
        }

        public XElement Save()
        {
            int rowCount = Style.Count;
            int columnCount = Style.First().Count;
            XElement element = new XElement("Style");
            element.SetAttributeValue("Row", rowCount.ToString());
            element.SetAttributeValue("Column", columnCount.ToString());
            element.SetAttributeValue("BorderThickness", borderStyle.BorderThickness.ToString());
            element.SetAttributeValue("BorderColor",SerializeCast.ColorToString(borderStyle.BorderColor));
            for (int i = 0; i < rowCount; ++i)
            {
                for (int j = 0; j < columnCount; ++j)
                {
                    XElement dataNode = GetUnit(i, j).CreateElement(i, j);
                    element.Add(dataNode);
                }
            }

            return element;
        }

        public void Load(XElement element)
        {
            int row = Convert.ToInt32(element.Attribute("Row").Value);
            int column = Convert.ToInt32(element.Attribute("Column").Value);
            if (element.Attribute("BorderThickness") != null)
            {
                borderStyle.BorderThickness = Convert.ToInt32(element.Attribute("BorderThickness").Value);
            }
            if (element.Attribute("BorderColor") != null)
            {
                borderStyle.BorderColor = SerializeCast.StringToColor(element.Attribute("BorderColor").Value);
            }
            for (int i = 0; i < row; ++i)
            {
                for (int j = 0; j < column; ++j)
                {
                    XElement[] node = element.Elements("StyleUnit").Where(result => result.Attribute("Row").Value == i.ToString() && result.Attribute("Column").Value == j.ToString()).ToArray();
                    if (node.Length>0)
                    {
                        string data = node[0].Value;
                        BooTableStyleUnit unit = new BooTableStyleUnit();
                        unit.Width = Convert.ToDouble(node[0].Attribute("Width").Value);
                        unit.Height = Convert.ToDouble(node[0].Attribute("Height").Value);
                        if (node[0].Element("FontColor")!=null)
                            unit.FontColor = SerializeCast.StringToColor(node[0].Element("FontColor").Value);
                        if (node[0].Element("FontSize")!=null)
                            unit.FontSize = Convert.ToInt32(node[0].Element("FontSize").Value);
                        if (node[0].Element("FontFamily") != null)
                            unit.FontFamily = node[0].Element("FontFamily").Value;
                        if (node[0].Element("VerticalAlignment") != null)
                            unit.VerticalAlignment = SerializeCast.StringToVerticalAlignment(node[0].Element("VerticalAlignment").Value);
                        if (node[0].Element("TextAlignment") != null)
                            unit.TextAlignment = SerializeCast.StringToUITextAlignment(node[0].Element("TextAlignment").Value);

                        XElement fontStyle= node[0].Element("FontStyle");
                        if ( fontStyle!= null)
                        {
                            if (fontStyle.Attribute("IsBold") != null)
                            {
                                unit.IsBold = Convert.ToBoolean(fontStyle.Attribute("IsBold").Value);
                                unit.IsItalic = Convert.ToBoolean(fontStyle.Attribute("IsItalic").Value);
                                unit.IsUnderline = Convert.ToBoolean(fontStyle.Attribute("IsUnderline").Value);
                                unit.IsStrikeout = Convert.ToBoolean(fontStyle.Attribute("IsStrikeout").Value);
                            }

                        }

                        unit.BackgroundColor = SerializeCast.StringToColor(node[0].Element("BackgroundColor").Value);
                        //if (node[0].Element("BorderStyle") != null)
                        {

                        }
                        SetStyle(unit, i, j);
                    }
                }
            }
        }

        public void DefineGrid(Grid grid)
        {
            grid.ColumnDefinitions.Clear();
            grid.RowDefinitions.Clear();

            for (int i = 0; i < Style.ElementAt(0).Count; ++i)
            {
                ColumnDefinition cd=new ColumnDefinition();
                cd.Width=new GridLength(Style.ElementAt(0).ElementAt(i).Style.Width);
                grid.ColumnDefinitions.Add(cd);
            }

            for (int j = 0; j < Style.Count; ++j)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(Style.ElementAt(j).ElementAt(0).Style.Height);
                grid.RowDefinitions.Add(rd);
            }
        }
        public BooTableStyleUnit GetUnit(int row, int column)
        {
            if (row >= 0 && column >= 0)
                return Style.ElementAt(row).ElementAt(column).Style;
            return null;
        }

        public void BindStyle(int row, int column,DependencyProperty bindProperty, Binding binding)
        {
            BooTableStyleUnit unit = GetUnit(row,column);
        }

        private String GetStringData(int row, int cloumn)
        {
            return "";
        }
    }
}
